package com.ejlchina.tool.network

import android.content.Context
import android.util.Log
import com.ejlchina.tool.bean.VersionResBean
import com.ejlchina.tool.util.Executor
import com.google.gson.Gson

const val GET_VERSION_PACKAGE = "/api/kitchen-refuse-api/control-panels/list"
const val GET_BUCKET_INDEX = "/api/kitchen-refuse-api/om-tool/bucket-info"
const val UPDATE_BUCKET_INDEX_FIX = "/api/kitchen-refuse-api/om-tool/update-buckets"
const val CHECK_IS_ALL_DEVICE_FIXED = "/api/kitchen-refuse-api/om-tool/mac-check"
object NetApi {

    fun availableVersions(hardWareNo: String, callback: (version: List<RemoteCtrlBoardVersion>) -> Unit) {
            NetHttpManager.getInstance().doRequestNet(GET_VERSION_PACKAGE,
                false,
                mapOf(Pair("hardWareNo",hardWareNo)),
                {res, errMsg, codeInt ->
                    Log.i("okhttp","code is -> $codeInt")
                    res?.let {
                        callback(it.toList(RemoteCtrlBoardVersion::class.java))
                    } ?: callback(arrayListOf())
                }, isGet = true, isAuth = true)
        }


    fun getVersion(callBack: (VersionResBean?) -> Unit) {
            NetHttpManager.getInstance().doRequestNet(
                GET_VERSION,
                false,
                mapOf(
                    "osType" to 1,
                    "type" to 21
                ),
                { res, errMsg, codeInt ->
                    res?.let {
                        callBack(it.toBean(VersionResBean::class.java))
                    } ?: callBack(null)
                },
                isGet = true,
                isAuth = true
            )
    }

    fun getBucketIndexInfo(deviceListString: String, callback: (version: List<BucketIndexInfo>?) -> Unit){
        NetHttpManager.getInstance().doRequestNet(GET_BUCKET_INDEX,false,
            mapOf("macs" to  deviceListString),
            {res, errMsg, codeInt ->
                res?.let {
                    callback(it.toList(BucketIndexInfo::class.java))
                } ?: callback(null)
            },
            isGet = true, isAuth = true
        )
    }

    fun updateBuckets(deviceNo: String,bucketList: List<BucketIndexUpdateReqBean> ,callback: (List<IndexFixUpdateFailBean>?) -> Unit){
        val bucketListString = Gson().toJson(bucketList)
        NetHttpManager.getInstance().doRequestNet(UPDATE_BUCKET_INDEX_FIX,false,
            mapOf("deviceNo" to  deviceNo, "buckets" to bucketListString),
            {res, errMsg, codeInt ->
                res?.let {
                    callback(it.toList(IndexFixUpdateFailBean::class.java))
                } ?: callback(null)
            },
            isGet = false, isAuth = true
        )
    }

    fun checkIsAllDeviceIndexed(macString: String, callback: (IndexFixCheckResBean?) -> Unit){
        NetHttpManager.getInstance().doRequestNet(CHECK_IS_ALL_DEVICE_FIXED,false,
            mapOf("macs" to  macString),
            {res, errMsg, codeInt ->
                Executor.runOnMain{
                    res?.let {

                        callback(it.toBean(IndexFixCheckResBean::class.java))
                    } ?: callback(null)
                }
            },
            isGet = true, isAuth = true
        )
    }

}